var BulkEntryView = Backbone.View.extend({
	initialize : function() {
		this.render();
	},
	
	render : function() {
		var t = this;

		var html = $("#bulk_entry_template").html();
		this.$el.html(html);
		//this.renderFlipButton();
		// this.renderSticky();

		
		var bulkInit = function(view){
//			$(".panel-body").eq(1).append(bulk_asset.el);
//			$("#container_fileload .field-priority").css("display", "none");
//			bulk_asset.el.attr("display", "none");
			// $(".panel-body").eq(1).append(bulk_asset.el);
//			$(".panel-body").eq(1)[0].childNodes[1].attr("display", "none");
//			$("#bulk_init + div").css("display", "none");
			$(".field-BULK_priority").parent().css("display", "none");
			
//			
//			$("form[fid|='form_BULK_fileload_1']").bind('fileuploadsubmit', function(e, data) {			
//
//			});

			$("form[fid|='form_BULK_fileload_1']").fileupload({
				url : util.sfbase+"/rest/fileUpload/upload",   //"upload", //"../../rest/fileUpload",//
				//dataType : 'json',
				add : function(e, data) {
					$("#progress").attr("class", "span3 .fileupload-processing .fileupload-loading");
					//data.submit();
					var jqXHR = data.submit().success(function(result, textStatus, jqXHR) {
						var val = jqXHR.responseText;
						console.log("responseText: " + val);
						console.log("result: " + result);
						// var val = "{\"flag\":\"0\",\"status\":\"SUBMITTED\",\"filename\":\"GSI_APP-20120830-164910.csv\",\"destination\":\"/upload\",\"size\":\"297\"}";
						// var obj = JSON.parse(val);
						var table = view.getTables().BULKLOAD;
						//table.saveRow(JSON.parse(result));
						result = JSON.parse(result);
						result.type = $("#BULK_type :selected").text();
						
						result.priority = $("#BULK_priority :selected").text();
						table.saveRow(result);
						
						if(ViewData.Login.usr){
							if(ViewData.Login.Bulk == 1){
								$(".panel-container[name|='Bulk'] th[data-field|='priority']").css("display", "block");
								$(".panel-container[name|='Bulk'] td[colname|='priority']").css("display", "block");
							}
						}else{
							$(".panel-container[name|='Bulk'] th[data-field|='priority']").css("display", "none");
							$(".panel-container[name|='Bulk'] td[colname|='priority']").css("display", "none");
						}
					}).error(function(jqXHR, textStatus, errorThrown) {
						alert("error");
						console.log(errorThrown);
					}).complete(function(result, textStatus, jqXHR) {
						console.log("complete");
//						setTimeout("$('#progress').attr('class','span3 fileupload-progress fade')", 2000);
					});
				},
				// Callback for upload progress events:
				progressall : function(e, data) {
					var progress = parseInt(data.loaded / data.total * 100, 10);
					$('#progress .bar').css('width', progress + '%');
					if (progress == 100){
						setTimeout("$('#progress').attr('class','span3 fileupload-progress fade');$('#progress .bar').css('width', '0%');", 2000);						
					}						
				},
				done : function(e, data) {
					$.each(data.result, function(index, file) {
						$('<p/>').text(file.name).appendTo(document.body);
					});
					console.log("done");					
				}
			});

			var formatDateVal = function(val){
				var arr = val.split("-");
				var newArr = new Array();
				newArr[0] = arr[2];
				newArr[1] = arr[1];
				newArr[2] = arr[0];
				var newVal = newArr.join("-");
				
				return newVal;
			}
			
			var formatVal = function(n,obj){
				obj.id = n;
				if (obj.type=="IAA") obj.type = "Issue Asset Add";
				else if (obj.type == "IAC") obj.type = "Issue Asset Change";
				else if (obj.type == "IAD") obj.type = "Issue Asset Drop";
				else if (obj.type == "QAC") obj.type = "Quote Asset Change";
				else if (obj.type == "QAR") obj.type = "Quote Asset Rename";
				else if (obj.type == "QAA") obj.type = "Quote Asset Add";
				else if (obj.type == "QAD") obj.type = "Quote Asset Drop";
				else if (obj.type == "ROA") obj.type = "ROOT Add";
				else if (obj.type == "ROC") obj.type = "ROOT Change";
				else if (obj.type == "ROD") obj.type = "ROOT Drop";
				else if (obj.type == "UCA") obj.type = "URIC Add";
				else if (obj.type == "UCC") obj.type = "URIC Change";
				else if (obj.type == "UCD") obj.type = "URIC Drop";
				else if (obj.type == "THA") obj.type = "Exchange Trading Hours Add";
				else if (obj.type == "THC") obj.type = "Exchange Trading Hours Change";
				else if (obj.type == "THD") obj.type = "Exchange Trading Hours Drop";
				
				
				if (obj.priority == "1") obj.priority = "HIGH";
				else if (obj.priority == "2") obj.priority = "MEDIUM";
				else if (obj.priority == "3") obj.priority = "LOW";
			}
			
			var fileLoadHistory = function() {

				var createby = $("#BULK_createby").val();
				//var createdate = "2012-09-04";
				var createdate,modifydate;
				if ($("#BULK_createdate_input").val()){
					createdate = formatDateVal($("#BULK_createdate_input").val());			
				}
				if ($("#BULK_modifydate_input").val()){
					modifydate = formatDateVal($("#BULK_modifydate_input").val());
				}
						
				var priority = $("#BULK_prioritySearch").val();

				var datastr = "{\"createby\":\"" + createby + "\",\"createdate\":\"" + createdate + "\",\"modifydate\":\"" + modifydate + "\",\"priority\":\"" + priority + "\"}";
				var data = JSON.parse(datastr);

				$.ajax({
					url : util.sfbase+"/rest/fileUpload/loadHistory",  // "loadHistory", //"../../rest/fileUpload/loadHistory",//"../../rest/fileUpload/userHistory",
					type : "POST",
					dataType : "json",
					data : data,
					error : function(xhr, ajaxOptions, thrownError) {
//						$.unblockUI();
						alert("error:" + thrownError);
						return false;
					},
					success : function(data) {
						if (!data){
							alert("No records to be retrieved!");
						}else if (data.flag == 0) {
							var val = data.val, table = view.getTables().BULKLOADHISTORY;
							table.setValue = function(value) {
								if (value && value.length > 0) {
									this.removeAll();
									//this.addFirst();
									$.each(value, formatVal);
									var collection = new BTS.SortableList(value);
									this.collection = collection;
									this.collection.forEach(this.add, this);
								} else {									
									this.removeAll();
									alert("No records to be retrieved!");
								}
							};
							if (val && val.length > 0) {
								table.setValue(val);
							} else {
								table.setValue([]);
							}
							
							if(ViewData.Login.usr){
								if(ViewData.Login.Bulk == 1){							
									$(".panel-container[name|='Bulk'] th[data-field|='priority']").css("display", "block");
									$(".panel-container[name|='Bulk'] td[colname|='priority']").css("display", "block");
								}
							}else{
								$(".panel-container[name|='Bulk'] th[data-field|='priority']").css("display", "none");
								$(".panel-container[name|='Bulk'] td[colname|='priority']").css("display", "none");
							}
			
						} else if (data.flag == 1) {
							alert(data.error.info);
						} else {
							alert("results format error");
						}
					}
				});
			};

			$("#BULK_btnFHSearch").bind('click', fileLoadHistory);

			var prio = $(".field-BULK_priority").parent();
			var userUpdate=function(){
				var prio = $(".field-BULK_priority").parent();
				var prioSearch = $(".field-BULK_prioritySearch").parent();
				
				prio.css("display", "none");
				prioSearch.css("display", "none");
				
				$(".panel-container[name|='Bulk'] th[data-field|='priority']").css("display", "none");
				$(".panel-container[name|='Bulk'] td[colname|='priority']").css("display", "none");
				
				if(ViewData.Login.usr){				
					view.activate(false);
					if(ViewData.Login.Bulk == 1){
						if (prio) prio.css("display", "");
						if (prioSearch) prioSearch.css("display", "");
						
						$(".panel-container[name|='Bulk'] th[data-field|='priority']").css("display", "");
						$(".panel-container[name|='Bulk'] td[colname|='priority']").css("display", "");
					}
				}else{
					view.activate(true);
					$(".panel-container[name|='Bulk'] th[data-field|='priority']").css("display", "none");
					$(".panel-container[name|='Bulk'] td[colname|='priority']").css("display", "none");
				}
			}			
		    prio.on({
				"user": userUpdate
			});
		    
		    var prioSearch = $(".field-BULK_prioritySearch").parent();    
		    
			Auth.ctrl.push(prio);	
			prio.trigger("user");			
			
			//prio.css("display", "none");
		};		
		$('#btnbulkload').click(function(){
			func.constructPage(true, bulkload, viewFactory.BulkBlockView(), null, bulkInit);			
		})
	}
});